{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "x=1\n",
    "y=2\n",
    "\n",
    "exec(\"r=x+y\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "script=\"\"\"\n",
    "def add(x:int,y:int):\n",
    "    return x+y\n",
    "\"\"\"\n",
    "\n",
    "exec(script)\n",
    "r = add(1,2)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "global_vars={\n",
    "    \"x\": 1,\n",
    "    \"y\": 2\n",
    "}\n",
    "script=\"\"\"\n",
    "def add(x:int,y:int):\n",
    "    return x+y\n",
    "print(add(x,y))\n",
    "\"\"\"\n",
    "exec(script,global_vars)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['a', '__builtins__', 'b'])\n"
     ]
    }
   ],
   "source": [
    "d={\n",
    "    \"a\":1\n",
    "}\n",
    "exec('b=2',d)\n",
    "print(d.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n"
     ]
    }
   ],
   "source": [
    "\n",
    "a=1\n",
    "b=2\n",
    "\n",
    "locale_vars={'a':10,'b':20}\n",
    "exec(\"z=a+b\\nprint(z)\",{},locale_vars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, zhangsan!\n"
     ]
    }
   ],
   "source": [
    "# 使用分号分隔多个 Python 语句\n",
    "exec(\"name = input('Your name: '); print(f'Hello, {name}!')\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果：3\n"
     ]
    }
   ],
   "source": [
    "string_input=\"\"\"\n",
    "def add(x,y):\n",
    "    return x+y\n",
    "print(f'结果：{add(x,y)}')\n",
    "\"\"\"\n",
    "locale_vars={\n",
    "    'x':1,\n",
    "    'y':2\n",
    "}\n",
    "compoiled_code = compile(string_input,'<string>','exec')\n",
    "exec(compoiled_code,None,locale_vars)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "__import__ not found",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[50], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mexec\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mimport os\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m__builtins__\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43m}\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32m<string>:1\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: __import__ not found"
     ]
    }
   ],
   "source": [
    "exec(\"import os\", {\"__builtins__\": {}}, {})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "\n",
    "def load_config(config_file:str):\n",
    "    config_file = Path(config_file)\n",
    "    with open(config_file,mode=\"r\",encoding=\"utf-8\")as fp:\n",
    "        source=fp.readlines()\n",
    "        compiled_code = compile(source,fp.name,\"exec\")\n",
    "        config_dict={}\n",
    "        exec(compiled_code,{\"__builtins__\": {}},config_dict)\n",
    "    return config_dict\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
